package domain;

import java.util.Date;

import physical.CardReader;
import physical.CashDispenser;
import physical.ReceiptPrinter;
import simulation.bankSim.AccountList;
import simulation.bankSim.AccountServiceSim;

/*
 * ATM作为控制类，先接収所有数据进行操作
 * 
 * 按逻辑顺序的操作
 * */
public class ATM {
	private Card card;
	private CardReader cardReader = new CardReader(); // 由ATM实例化读卡机
	// 由ATM实例化远程模拟的银行系统
	private AccountServiceSim accountServiceSim = new AccountServiceSim();
	private Session session; // 一次会话，这里不是很懂
	private Transaction transaction; // 一次事务
	private CashDispenser cashDispenser = new CashDispenser(); // 提款机
	private ReceiptPrinter receiptPrinter = new ReceiptPrinter();
	private Receipt receipt;
	private Log log = new Log();

	public void turnOn() {
		System.out.println("ATM已启动");
		log.writeToFile(""); // 空一行方便看
		log.writeToFile("开启一次服务"); // 日志记录
		log.writeToFile("ATM已经启动");
		
	}

	/*
	 * 客户插卡操作
	 * 
	 * 输入：卡号（String） 返回：卡的实例（Card）
	 * 
	 * 不是ATM的职责，调用Card类进行实例化
	 */
	public Card pluginCard(String cardNo) {
		card = cardReader.readCard(cardNo); // 由读卡机实例化卡
		log.writeToFile("卡号：" + card.getCardNo() + "实例已创建");
		return card;
	}

	/*
	 * 输入密码操作
	 * 
	 * 输入：密码(String) 返回：Session实例(Session)
	 * 
	 * 不是ATM的职责，调用session类进行实例化
	 */
	public Session getSessionAfterIdentify(Card card, String password) {
		if (accountServiceSim.verification(card.getCardNo(), password).isStatus()) {
			session = new Session(card, new Date()); // 远程银行系统实例化session
			log.writeToFile("卡号：" + card.getCardNo() + "验证成功，创建session");
		}
		return session;
	}

	/*
	 * 选取取款业务操作
	 * 
	 * 输入：业务编号(String) 返回：Transaction实例(Transaction)
	 * 
	 * 不是ATM的职责，调用Transaction类进行实例化
	 */
	public Transaction selectBusiness(int businessNo) {
		transaction = new Transaction(businessNo);
		log.writeToFile("创建一次交易");
		return transaction;
	}

	/*
	 * 输入取款金额的操作
	 * 
	 * 输入：卡的实例(Card)，取款金额（int)
	 * 
	 */
	public boolean withdrawal(Card card, int amount) {
		if (accountServiceSim.withdrawal(card.getCardNo(), amount).isStatus()) {
			System.out.println("操作成功！");
			log.writeToFile("卡号：" + card.getCardNo() + "取款成功");
			return true;
		} else {
			System.out.println("操作失败！");
			log.writeToFile("卡号：" + card.getCardNo() + "取款失败");
			return false;
		}
	}

	/*
	 * 取走现金
	 * 
	 * 返回：取走现金语句
	 * 
	 * 不是ATM的职责，调用CashDispenser类进行操作
	 */
	public void getCash(int amount) {
		cashDispenser.payout(amount);
		log.writeToFile("出钞成功，可拿走现金");
		
	}

	/*
	 * 打印凭条操作
	 * 
	 * 输入：是否打印凭条 返回：打印凭条语句
	 * 
	 * 不是ATM的职责，调用Receipt类进行实例化
	 */
	public void printReceipt(Card card, int amount) {
		Date date = new Date();
		receipt = receiptPrinter.print(card, date, amount);

		System.out.println("时间：" + receipt.getDate());
		System.out.println("业务：" + receipt.getBusiness());
		System.out.println("卡号：" + receipt.getCard().getCardNo());
		System.out.println("取款金额：" + receipt.getAmount() + "元");
		
		log.writeToFile("打印凭条成功");
	}

	/*
	 * 退卡操作
	 */
	public void dropOut(Card card) {
		cardReader.dropOut(card);
		log.writeToFile("卡号：" + card.getCardNo() + "退卡成功");
	}

	/*
	 * 吞卡操作
	 */
	public void swallowCard(Card card) {
		cardReader.swallowCard(card);
		log.writeToFile("卡号：" + card.getCardNo() + "已锁定");
	}

	public void turnOff() {
		System.out.println("ATM已关闭");
		log.writeToFile("ATM已关闭");
	}

}
